package com.wangc.report.service.impl;

import com.wangc.report.dao.SysMenuMapper;
import com.wangc.report.entity.SysMenuEntity;
import com.wangc.report.service.SysMenuService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 菜单管理Service实现类
 *
 * @author wangc
 * @email 1620415297@qq.com
 * @date 2020-04-01 23:34:07
 */
@Service("sysMenuService")
public class SysMenuServiceImpl implements SysMenuService {
    private static Logger logger = LoggerFactory.getLogger(SysMenuServiceImpl.class);
    @Autowired
    private SysMenuMapper sysMenuMapper;

    @Override
    public SysMenuEntity queryObject(Long menuId) {
        return sysMenuMapper.selectByPrimaryKey(menuId);
    }

    @Override
    public List<SysMenuEntity> queryList(Map<String, Object> map) {
        return sysMenuMapper.queryList(map);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return sysMenuMapper.queryTotal(map);
    }

    @Override
    public int save(SysMenuEntity sysMenu) {
        return sysMenuMapper.insertSelective(sysMenu);
    }

    @Override
    public int update(SysMenuEntity sysMenu) {
        return sysMenuMapper.updateByPrimaryKeySelective(sysMenu);
    }

    @Override
    public int delete(Long menuId) {
        return sysMenuMapper.deleteByPrimaryKey(menuId);
    }

    @Override
    public int deleteBatch(Long[] menuIds) {
        return sysMenuMapper.deleteBatch(menuIds);
    }

    @Override
    public List<SysMenuEntity> getUserMenuList(Long userId) {

        //用户菜单列表
        List<Long> menuIdList = sysMenuMapper.queryAllMenuId(userId);
        return getAllMenuList(menuIdList);
    }

    /**
     * 获取所有菜单列表
     */
    private List<SysMenuEntity> getAllMenuList(List<Long> menuIdList) {
        //查询根菜单列表
        List<SysMenuEntity> menuList = queryListParentId(0L, menuIdList);
        //递归获取子菜单
        getMenuTreeList(menuList, menuIdList);

        return menuList;
    }

    public List<SysMenuEntity> queryListParentId(Long parentId, List<Long> menuIdList) {
        List<SysMenuEntity> menuList = sysMenuMapper.queryListParentId(parentId);
        if (menuIdList == null) {
            return menuList;
        }

        for (SysMenuEntity sysMenuEntity : menuList) {
            logger.debug("sysMenuEntity:" + sysMenuEntity.toString());
        }
        List<SysMenuEntity> userMenuList = new ArrayList<>();
        for (SysMenuEntity menu : menuList) {
            if (menuIdList.contains(menu.getMenuId())) {
                userMenuList.add(menu);
            }
        }
        logger.debug("userMenuList:" + userMenuList.toString());
        return userMenuList;
    }

    /**
     * 递归
     */
    private List<SysMenuEntity> getMenuTreeList(List<SysMenuEntity> menuList, List<Long> menuIdList) {
        List<SysMenuEntity> subMenuList = new ArrayList<SysMenuEntity>();

        for (SysMenuEntity entity : menuList) {
            if (entity.getType() == 0) {//目录
                entity.setList(getMenuTreeList(queryListParentId(entity.getMenuId(), menuIdList), menuIdList));
            }
            subMenuList.add(entity);
        }
        logger.debug("subMenuList:" + subMenuList.toString());
        return subMenuList;
    }
}
